package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.api.Api;

import org.json.JSONException;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class rechargeActivity70 extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private EditText et_game70_re_number;
    private TextView tv_game70_re_10;
    private TextView tv_game70_re_num;
    private TextView tv_game70_re_50;
    private TextView tv_game70_re_100;
    private EditText et_game70_re_price;
    private Button btn_game70_re_submit;
    private RequestQueue queue;
    private ProgressDialog show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge70);
        queue = Volley.newRequestQueue(this);
        initView();
        //监听et输入值
        et_game70_re_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String val = et_game70_re_price.getText().toString();
                if(val.equals("10")){
                    tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                    tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_back_70);
                }else if(val.equals("50")){
                    tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                    tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_back_70);
                }else if(val.equals("100")) {
                    tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                }else {
                    tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_back_70);
                    tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_back_70);
                }
            }
        });

        //监听车牌编号监听
        et_game70_re_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!s.toString().equals("") || !TextUtils.isEmpty(s.toString())){
                    getCarNumber();
                }
            }
        });


    }

    public void getCarNumber(){
        show();
        String val = et_game70_re_number.getText().toString();
        JSONObject object = new JSONObject();
        try {
            object.put("id",val);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "ETCSelectCarNumbser", object.toString(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                try {
                    String data = jsonObject.getString("data");
                    if(data == null || data.equals("null") || TextUtils.isEmpty(data)){
                        tv_game70_re_num.setText("");
                    }
                    tv_game70_re_num.setText(data);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(rechargeActivity70.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);
    }

    private void show(){
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    private void hide(){
        show.dismiss();
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_game70_re_num = (TextView) findViewById(R.id.tv_game70_re_num);

        tv_content_title.setText("ETC充值");
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        et_game70_re_number = (EditText) findViewById(R.id.et_game70_re_number);
        tv_game70_re_10 = (TextView) findViewById(R.id.tv_game70_re_10);
        tv_game70_re_10.setOnClickListener(this);
        tv_game70_re_50 = (TextView) findViewById(R.id.tv_game70_re_50);
        tv_game70_re_50.setOnClickListener(this);
        tv_game70_re_100 = (TextView) findViewById(R.id.tv_game70_re_100);
        tv_game70_re_100.setOnClickListener(this);
        et_game70_re_price = (EditText) findViewById(R.id.et_game70_re_price);
        btn_game70_re_submit = (Button) findViewById(R.id.btn_game70_re_submit);
        btn_game70_re_submit.setOnClickListener(this);
    }

    private void savePrice(){
        show();
        //获取车牌号
        String num = et_game70_re_number.getText().toString();
        String price = et_game70_re_price.getText().toString();

        JSONObject object = new JSONObject();
        try {
            object.put("id",num);
            object.put("price",price);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "ETCrecharge", object.toString(), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                try {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String status = data.getString("status");

                    if(status.equals("ok")){
                        Toast.makeText(rechargeActivity70.this, "充值成功！", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(rechargeActivity70.this, "充值失败！", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(rechargeActivity70.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_game70_re_submit:


                savePrice();


                break;
            case R.id.tv_game70_re_10:
                tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_back_70);
                tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_back_70);
                et_game70_re_price.setText("10");
                break;
            case R.id.tv_game70_re_50:
                tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_back_70);
                tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_back_70);
                et_game70_re_price.setText("50");
                break;
            case R.id.tv_game70_re_100:
                tv_game70_re_10.setBackgroundResource(R.drawable.recharge_text_back_70);
                tv_game70_re_50.setBackgroundResource(R.drawable.recharge_text_back_70);
                tv_game70_re_100.setBackgroundResource(R.drawable.recharge_text_red_70_back);
                et_game70_re_price.setText("100");

                break;
        }
    }

}
